package org.devocean.groupbuying.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Plan {

	@Id
	@GeneratedValue
	private Integer planID;
	private String url;
	private Integer trial;

	@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinColumn(name = "articlesLocalizationID", referencedColumnName = "articlesLocalizationID", nullable = false)
	private ArticlesLocalization articlesLocalization;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "plan", cascade = CascadeType.ALL)
	private Set<Formula> formulas = new HashSet<Formula>();

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "plan", cascade = CascadeType.ALL)
	private Set<Promotion> promotions = new HashSet<Promotion>();

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "productID", referencedColumnName = "productID")
	private Product product;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "PlanCompatibilities", joinColumns = { @JoinColumn(name = "planID", referencedColumnName = "planID") }, inverseJoinColumns = { @JoinColumn(name = "compatibilityID", referencedColumnName = "compatibilityID") })
	private Set<Compatibility> compatibilities = new HashSet<Compatibility>();

	public Integer getPlanID() {
		return planID;
	}

	public boolean add(Promotion e) {
		return promotions.add(e);
	}

	public void setPlanID(Integer planID) {
		this.planID = planID;
	}

	public ArticlesLocalization getArticlesLocalization() {
		return articlesLocalization;
	}

	public void setArticlesLocalization(
			ArticlesLocalization articlesLocalization) {
		this.articlesLocalization = articlesLocalization;
	}

	public Set<Formula> getFormulas() {
		return formulas;
	}

	public void setFormulas(Set<Formula> formulas) {
		this.formulas = formulas;
		for (Formula formula : formulas) {
			if (formula.getPlan() == null) {
				formula.setPlan(this);
			}

		}

	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public Set<Promotion> getPromotions() {
		return promotions;
	}

	public void setPromotions(Set<Promotion> promotions) {
		this.promotions = promotions;
	}

	public Set<Compatibility> getCompatibilities() {
		return compatibilities;
	}

	public void setCompatibilities(Set<Compatibility> compatibilities) {
		this.compatibilities = compatibilities;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public String toString() {
		return "Plan [planID=" + planID + "]";
	}

	public Integer getTrial() {
		return trial;
	}

	public void setTrial(Integer trial) {
		this.trial = trial;
	}

}
